<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :table-loading="loading"
               :permission="permissionList"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               :page="page"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="name"
                slot-scope="{row,size}">
        <el-tag :size="size">{{row.name}}</el-tag>
      </template>
      <template slot-scope="{row,type,size}"
                slot="menu">
        <el-button :type="type"
                   :size="size"
                   icon="el-icon-thumb"
                   @click="handleOpen(row)" >字典集合</el-button>
      </template>
    </avue-crud>

    <el-dialog title="字典集合"
               :visible.sync="box"
               width="70%">
      <dict v-if="box"
            :dictTypeId="dictTypeId"></dict>
    </el-dialog>
  </basic-container>
</template>

<script>
import dict from '../../admin/dict';
import {
  getList,
  getDictType,
  remove,
  update,
  add,
} from "@/api/admin/dictType";
import { mapGetters } from "vuex";
import {
    testSort
} from '@/util/util';
export default {
  components: {
    dict
  },
  data () {
    return {
      form: {},
      query: {},
      box: false,
      dictTypeId: '',
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        size: 'mini',
        tip: false,
        border: true,
        index: true,
        selection: false,
        viewBtn: true,
        editBtn:true,
        "dialogType": "dialog",
        height: 'auto',
        align: 'center',
        // calcHeight: 380,
        menuWidth: 300,
        rowKey: 'dictTypeId',
        searchMenuSpan:6,
        column: [
          {
            label: "类型名称",
            prop: "name",
            search: true,
            fixed: true,
            slot: true,
            maxlength:20,
            rules: [{
              required: true,
              message: "请输入类型名称",
              trigger: "blur"
            }]
          },
          {
            label: "类型编码",
            prop: "code",
            span: 24,
            maxlength:50,
            rules: [{
              required: true,
              message: "请输入类型编码",
              trigger: "blur"
            }]
          },
          {
            label: "系统字典",
            prop: "systemFlag",
            type: 'select',
            rules: [{
              required: true,
              message: "请选择系统字典",
              trigger: "blur"
            }],
            dicData: [
              {
                label: "是",
                value: 'Y'
              },
              {
                label: "否",
                value: 'N'
              }
            ],
          },
          {
            label: "排序",
            prop: "sort",
            type: 'input',
            rules: [{
              required: true,
              message: "请输入排序",
              trigger: "blur"
            },
            {
                validator: testSort,
                trigger: 'blur'
            },
            ]
          },
          {
            label: "备注",
            prop: "description",
            type: 'textarea',
            span: 24,
            minRows: 5,
            hide: true,
            rules: [{
              required: true,
              message: "请输入备注",
              trigger: "blur"
            }],
          },
          {
            label: "时间",
            prop: "createTime",
            addDisplay: false,
            disabled: true,
			width: 160,
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList () {
      return {
        addBtn: this.vaildData(this.permission["/mall-cloud-system/dict/addItem"], true),
        viewBtn: this.vaildData(this.permission["/mall-cloud-system/dict/detail"], true),
        delBtn: this.vaildData(this.permission["/mall-cloud-system/dict/delete"], true),
        editBtn: this.vaildData(this.permission["/mall-cloud-system/dict/editItem"], true)
      };
    },
  },
  methods: {
    handleOpen (row) {
      this.dictTypeId = row.dictTypeId;
      this.box = true;
    },
    rowSave (row, loading, done) {
      add(row).then(res => {
        if(res.data.success){
            loading();
            this.onLoad(this.page);
            this.$message({
                type: "success",
                message: "操作成功!"
            });
        }
        else{
            done();
        }

      }, error => {
        done();
        ;
      });
    },
    rowUpdate (row, index, loading, done) {
      update(row).then(res => {
        if(res.data.success){
            loading();
            this.onLoad(this.page);
            this.$message({
                type: "success",
                message: "操作成功!"
            });
        }
        else{
            done();
        }
      }, error => {
        done();
        ;
      });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.dictTypeId);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange (params,done) {
      this.page.currentPage=1
      this.query = params;
      this.onLoad(this.page, params,done);
    },
    beforeOpen (done, type) {
      if (["edit", "view"].includes(type)) {
        getDictType(this.form.dictTypeId).then(res => {
          this.form = res.data.data;
        });
      }
      done();

    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange () {
      this.onLoad(this.page);
    },
    onLoad (page, params = {},done) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        if(done){
          done();
        }
        this.loading = false;
        const data = res.data;
        this.page.total = data.count;
        this.data = data.data;
        //为了在删除最后一页的最后一条数据时能成功跳转回最后一页的上一页
        if(data.data.length === 0 && this.page.currentPage > 0) {
          this.page.currentPage -= 1;
          this.onLoad(this.page);
        }
      });
    }
  }
};
</script>

<style>
</style>
